﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace RoslynProxy.Builder
{
    public class CSharpProgram
    {
        public static SyntaxTree[] GetSyntaxTrees()
        {
            FileInfo fileInfo = new FileInfo(typeof(CSharpProgram).Assembly.Location);
            var currentSourceDir = Path.Combine(fileInfo.Directory!.FullName, "..\\..\\..\\");
            var corDir = Path.Combine(currentSourceDir, "..\\..\\src\\RoslynProxy.Core");
            var contractsDir = Path.Combine(currentSourceDir, "..\\..\\src\\RoslynProxy.Core\\RoslynProxyAttributes");

            var files1 = ReadCsFilesInDirectory(currentSourceDir);
            var files2 = ReadCsFilesInDirectory(corDir);
            var files3 = ReadCsFilesInDirectory(contractsDir);

            var files = new List<SyntaxTree>();

            files.AddRange(files1.Select(item => CSharpSyntaxTree.ParseText(File.ReadAllText((Path.GetFullPath(item))))));
            files.AddRange(files2.Select(item => CSharpSyntaxTree.ParseText(File.ReadAllText((Path.GetFullPath(item))))));
            files.AddRange(files3.Select(item => CSharpSyntaxTree.ParseText(File.ReadAllText((Path.GetFullPath(item))))));

            return files.ToArray();
        }



        static string[] ReadCsFilesInDirectory(string path)
        {
            // 获取目录下的所有文件和子目录  
            return Directory.GetFiles(path, "*.cs");
        }
    }
}
